package com.zhiyou100.video.webaction.Ajax;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhiyou100.video.model.ResultMessage;
import com.zhiyou100.video.model.User;
import com.zhiyou100.video.service.CourseService;
import com.zhiyou100.video.service.SpeakerService;
import com.zhiyou100.video.service.UserService;
import com.zhiyou100.video.service.VideoService;
import com.zhiyou100.video.utils.MD5Utils;
@SuppressWarnings("serial")
@Controller("AjaxAction")
@Scope(scopeName="prototype")
public class AjaxAction extends ActionSupport implements ModelDriven<User>{
	@Override
	public User getModel() {
		return upe;
	}
	private Integer did;
	private ResultMessage rm;
	private User upe = new User();
	private String oldPassword;
	private String newPassword;
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public ResultMessage getRm() {
		return rm;
	}

	public void setRm(ResultMessage rm) {
		this.rm = rm;
	}

	public Integer getDid() {
		return did;
	}

	public void setDid(Integer did) {
		this.did = did;
	}
	@Autowired
	SpeakerService ss;
	public String deletespeaker() throws Exception {
		ss.deletespeaker(did);
		rm = new ResultMessage();
		rm.setResu("success");
		return "success";
	}
	@Autowired
	CourseService cs;
	public String deletecourse() throws Exception {
		cs.deletecourse(did);
		rm = new ResultMessage();
		rm.setResu("success");
		return "success";
	}
	@Autowired
	VideoService vs;
	public String deletevideo() throws Exception {
		vs.deleteVideo(did);
		 rm = new ResultMessage();
		rm.setResu("success");
		return "success";
	}
	@Autowired
	UserService us;
	public String userregister() throws Exception {
		upe.setInsertTime(new Date(System.currentTimeMillis()));
		upe.setPassword(MD5Utils.getMD5(upe.getPassword()));
		us.addRegister(upe);
		rm = new ResultMessage();
		rm.setResu("success");
			return SUCCESS;
		}
	public String userlogin() throws Exception {
		rm = new ResultMessage();
		DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		dc.add(Restrictions.eq("password",MD5Utils.getMD5(upe.getPassword()))).add(Restrictions.eq("email", upe.getEmail()));
		List<User> list =us.selectUser(dc);
		if(list.isEmpty()){
			rm.setResu("error");
			return SUCCESS;
		}
		ActionContext.getContext().getSession().put("_front_user", list.get(0));
		rm.setResu("success");
		return SUCCESS;
	}
	/*修改密码*/
	public String topassword() {
		
		
		rm = new ResultMessage();
		DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		dc.add(Restrictions.eq("id", upe.getId())).add(Restrictions.eq("password",MD5Utils.getMD5(oldPassword)));
		List<User> list =us.selectUser(dc);
		System.out.println(list);
		if(list.isEmpty()){
			rm.setResu("error");
			return SUCCESS;
		}
		upe.setPassword(MD5Utils.getMD5(newPassword));
		us.updatePassWord(upe);
		ActionContext.getContext().getSession().remove("_front_user");
		rm.setResu("success");
		return SUCCESS;
		
	}
	public String sendemail(){
		us.updateSendemail(upe);
		rm = new ResultMessage();
		rm.setResu("success");
		return SUCCESS;
		
	}
	public String forgetpwd(){
		rm = new ResultMessage();
		DetachedCriteria dc = DetachedCriteria.forClass(User.class);
		dc.add(Restrictions.eq("email", upe.getEmail())).add(Restrictions.eq("captcha", upe.getCaptcha()));
		List<User> list = us.selectUser(dc);
		if(list.isEmpty()){
			rm.setResu("error");
			return SUCCESS;
		}
		rm.setResu("success");
		rm.setUid(list.get(0).getId());
		return SUCCESS;
		
	}
	
}
